package com.apinji.alipay.sdk;

import com.apinji.common.plugin.StrUtils;
import com.apinji.unionpay.sdk.LogUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public final class AlipaySdkConfig {

    public static final String FILE_NAME = "alipay_sdk.properties";

    private static AlipaySdkConfig instance = new AlipaySdkConfig();

    private AlipaySdkConfig() {
        super();
        loadPropertiesFromSrc();
    }


    public static AlipaySdkConfig getInstance() {
        return instance;
    }


    private String appId;
    private String merchantPrivateKey;
    private String alipayPublicKey;
    private String notifyUrl;
    private String returnUrl;
    private String signType;
    private String charset;
    private String gatewayUrl;


    private void loadSdkConfig(Properties pro) {
        LogUtil.writeLog("开始从属性文件中加载配置项");
        String value = null;
        value = pro.getProperty("alipay.appid");
        if (!StrUtils.isEmpty(value)) {
            this.appId = value.trim();
        }

        value = pro.getProperty("alipay.merchant.private.key");
        if (!StrUtils.isEmpty(value)) {
            this.merchantPrivateKey = value.trim();
        }

        value = pro.getProperty("alipay.public.key");
        if (!StrUtils.isEmpty(value)) {
            this.alipayPublicKey = value.trim();
        }

        value = pro.getProperty("alipay.notify.url");
        if (!StrUtils.isEmpty(value)) {
            this.notifyUrl = value.trim().replace("/", File.separator);
        }

        value = pro.getProperty("alipay.return.url");
        if (!StrUtils.isEmpty(value)) {
            this.returnUrl = value.trim().replace("/", File.separator);
        }

        value = pro.getProperty("alipay.sign.type");
        if (!StrUtils.isEmpty(value)) {
            this.signType = value.trim();
        }

        value = pro.getProperty("alipay.charset");
        if (!StrUtils.isEmpty(value)) {
            this.charset = value.trim();
        }

        value = pro.getProperty("alipay.gateway.url");
        if (!StrUtils.isEmpty(value)) {
            this.gatewayUrl = value.trim();
        }
    }



    /**
     * 从classpath路径下加载配置参数
     */
    public void loadPropertiesFromSrc() {
        Properties properties = null;
        InputStream in = null;
        try {
            LogUtil.writeLog("从classpath: " +AlipaySdkConfig.class.getClassLoader().getResource("").getPath()+" 获取属性文件"+FILE_NAME);
            in = AlipaySdkConfig.class.getClassLoader().getResourceAsStream(FILE_NAME);
            if (null != in) {
                properties = new Properties();
                try {
                    properties.load(in);
                } catch (IOException e) {
                    throw e;
                }
            } else {
                LogUtil.writeErrorLog(FILE_NAME + "属性文件未能在classpath指定的目录下 "+AlipaySdkConfig.class.getClassLoader().getResource("").getPath()+" 找到!");
                return;
            }
            loadSdkConfig(properties);
        } catch (IOException e) {
            LogUtil.writeErrorLog(e.getMessage(), e);
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    LogUtil.writeErrorLog(e.getMessage(), e);
                }
            }
        }
    }



    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getMerchantPrivateKey() {
        return merchantPrivateKey;
    }

    public void setMerchantPrivateKey(String merchantPrivateKey) {
        this.merchantPrivateKey = merchantPrivateKey;
    }

    public String getAlipayPublicKey() {
        return alipayPublicKey;
    }

    public void setAlipayPublicKey(String alipayPublicKey) {
        this.alipayPublicKey = alipayPublicKey;
    }

    public String getNotifyUrl() {
        return notifyUrl;
    }

    public void setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
    }

    public String getReturnUrl() {
        return returnUrl;
    }

    public void setReturnUrl(String returnUrl) {
        this.returnUrl = returnUrl;
    }

    public String getSignType() {
        return signType;
    }

    public void setSignType(String signType) {
        this.signType = signType;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public String getGatewayUrl() {
        return gatewayUrl;
    }

    public void setGatewayUrl(String gatewayUrl) {
        this.gatewayUrl = gatewayUrl;
    }
}

